package com.simon.hse.activity.danger;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.CommonInput;
import com.simon.hse.http.entity.DangerEliminateOutput;
import com.simon.hse.utils.DateTimeUtils;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

/**
 * 不同区域-隐患排查任务
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_danger_area_task)
public class DangerAreaTaskActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.et_danger_eliminate_area_person)
    EditText mEt_danger_eliminate_area_person;

    @ViewInject(R.id.smartrefresh)
    SmartRefreshLayout mSmartRefresh;

    @ViewInject(R.id.recyclerview)
    RecyclerView mRecyclerView;

    private int currentPage = 1, totalPage = 1;
    private CommonEmptyAdapter mCommonEmptyAdapter;
    private List<DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean> mDataBeanList = new ArrayList<>();
    private String person;// 搜索数据
    private boolean isEditSearch = false;// 是否是搜索数据

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTv_common_title.setText("隐患排查任务");

        initRecyclerView();
        initData();
    }

    @Event(value = {R.id.iv_common_left})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
        }
    }

    private void initRecyclerView() {
        mEt_danger_eliminate_area_person.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                person = charSequence.toString();
                // initData();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        mSmartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (currentPage * 10 < totalPage) {
                    currentPage++;
                    initData();
                } else {
                    mSmartRefresh.finishLoadMoreWithNoMoreData();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                mDataBeanList.clear();
                initData();
            }
        });

        LinearLayoutManager llm = new LinearLayoutManager(DangerAreaTaskActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        mCommonEmptyAdapter = new CommonEmptyAdapter(DangerAreaTaskActivity.this, R.layout.layout_item_dagner_eliminate_area, mDataBeanList);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean>() {
            @Override
            public void setItemData(CommonViewHolder holder, DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean dataBean) {
                TextView tv_danger_eliminate_area_area = holder.getView(R.id.tv_danger_eliminate_area_area);
                TextView tv_danger_eliminate_area_state = holder.getView(R.id.tv_danger_eliminate_area_state);
                TextView tv_danger_eliminate_area_person = holder.getView(R.id.tv_danger_eliminate_area_person);
                TextView tv_danger_eliminate_area_time = holder.getView(R.id.tv_danger_eliminate_area_time);

                tv_danger_eliminate_area_area.setText(dataBean.getRegionText());
                String state = dataBean.getScreeningPlanProject(); // (0未开始 1进行中 2已完成)
                switch (state) {
                    case "0":
                        tv_danger_eliminate_area_state.setText("未开始");
                        tv_danger_eliminate_area_state.setBackgroundResource(R.drawable.rect_dark_corner_5);
                        break;
                    case "1":
                        tv_danger_eliminate_area_state.setText("进行中");
                        break;
                    case "2":
                        tv_danger_eliminate_area_state.setText("已完成");
                        break;
                    default:
                        tv_danger_eliminate_area_state.setText("未开始");
                        tv_danger_eliminate_area_state.setBackgroundResource(R.drawable.rect_dark_corner_5);
                        break;
                }
                tv_danger_eliminate_area_person.setText("负责人：" + dataBean.getPersonInChargeText());
                tv_danger_eliminate_area_time.setText("任务开始时间：" + DateTimeUtils.convertCustomDateYMD(dataBean.getBeginTime()));
            }
        });

        mCommonEmptyAdapter.setOnItemEmptyClickListener(new CommonEmptyAdapter.OnItemEmptyClickListener() {
            @Override
            public void setOnEmptyClickListener() {
                initData();
            }

            @Override
            public void setOnItemClickListener(View view, int position) {
                DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean projectBean = mDataBeanList.get(position);
                DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean.ScreeningPlanProjectDetailsBean projectDetails = projectBean.getScreeningPlanProjectDetails();
                Intent intent = new Intent(DangerAreaTaskActivity.this, DangerEliminateDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("THIRD_LEVEL", projectDetails);
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
    }

    private void initData() {
        /*Bundle bundle = getIntent().getExtras();
        if (bundle == null)
            return;
        DangerEliminateOutput.DataBean.PageDataBean pageDataBean = (DangerEliminateOutput.DataBean.PageDataBean) bundle.getSerializable("SECOND_LEVEL");
        if (pageDataBean != null) {
            List<DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean> yh_screeningPlanProject = pageDataBean.getYH_ScreeningPlanProject();
            mDataBeanList.addAll(yh_screeningPlanProject);
            mCommonEmptyAdapter.notifyDataSetChanged();
        }*/
        CommonInput commonInput = new CommonInput();
        CommonInput.ModelBean modelBean = new CommonInput.ModelBean();
        commonInput.setModel(modelBean);
        commonInput.setLimit(10);
        commonInput.setOffset(10 * (currentPage - 1));

        Call<String> call = HttpManager.getInstance().requestService().DANGER_ELIMINATE_LIST(HttpManager.getInstance().requestBody(commonInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                DangerEliminateOutput dangerEliminateOutput = new Gson().fromJson(result, DangerEliminateOutput.class);
                if (dangerEliminateOutput.getResult().equals("Success")) {
                    totalPage = dangerEliminateOutput.getData().getRowCount();
                    List<DangerEliminateOutput.DataBean.PageDataBean> dataBeanList = dangerEliminateOutput.getData().getPageData();
                    for (DangerEliminateOutput.DataBean.PageDataBean pageDataBean : dataBeanList) {
                        mDataBeanList.addAll(pageDataBean.getYH_ScreeningPlanProject());
                    }
                    mCommonEmptyAdapter.notifyDataSetChanged();

                    mSmartRefresh.finishRefresh();
                    mSmartRefresh.finishLoadMore();
                } else {
                    ToastUtils.getInstance().showShortToastBottom(dangerEliminateOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }
}
